#include <iom32.h>
#include "USART_func.h"

unsigned char USART_Receive( void )
{
  unsigned char c;
/* Wait for data to be received */
while ( !(UCSRA & (1<<RXC)) )
;
/* Get and return received data from buffer */
c=UDR;
return c;
}

void USART_Transmit(unsigned char data){

  //Wait for empty transmit buffer
  while (!(UCSRA & (1<<UDRE)));
  
  //Put data into buffer, sends the data
  UDR = data;

}

void USART_init (unsigned int baud){
  //Set baud rate
  UBRRH = (unsigned char)(baud>>8);
  UBRRL = (unsigned char)baud;
  //Enable receiver and transmitter and Rx_interrupt
  UCSRB = (1<<(RXEN))|(1<<TXEN)|(1<<(RXCIE));
  //Set frame format: 8data, 1stop bit
  UCSRC = (1<<URSEL)|(0<<USBS)|(3<<UCSZ0); 
}

